import 'dart:collection';
import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:my_flu01/common/MyButton.dart';

import '../common/SelectWidget.dart';

//https://blog.csdn.net/weixin_46136019/article/details/129706634
class MyButtonPage extends StatefulWidget {
  const MyButtonPage({Key? key}) : super(key: key);

  @override
  State<MyButtonPage> createState() => _MyButtonPage();
}

class _MyButtonPage extends State<MyButtonPage> {
  final String _title = "临风hanson HomePage";
  int selectedValue=1;
  String value = "1";

  /// 下拉选择值改变
  selectChange(value) {
    print("值改变了：$value");
  }


  @override
  initState() {
    super.initState();
    //_init();
  }

  @override
  Widget build(BuildContext context) {
    List<DropdownMenuItem<String>> sortItems = [];
    String _selectedSort = '排序';

    sortItems.add(DropdownMenuItem(
      value: '排序',
      child: Text('排序'),
      onTap: () {},
    ));
    sortItems.add(
        DropdownMenuItem(value: '价格降序', child: Text('价格降序'), onTap: () {}));
    sortItems.add(
        DropdownMenuItem(value: '价格升序', child: Text('价格升序'), onTap: () {}));

    return Scaffold(
        appBar: AppBar(
          title: Text("listView"),
        ),
        body: Center(
          child: Column(
              mainAxisAlignment: MainAxisAlignment.start, //layout
              children: <Widget>[
                ElevatedButton(
                  child: Text("ElevatedButton-sheetDialog"),
                  onPressed: () {
                    showButtonSheet();
                  },
                ),
                TextButton(
                  child: Text("TextButton-sheetDialog2"),
                  onPressed: () {
                    iosSheetShow();
                  },
                ),
                OutlinedButton(
                  child: Text("OutlinedButton-selectDialog"),
                  onPressed: () {
                    simpleDialog(context);
                  },
                ),
                IconButton(
                  icon: Icon(Icons.thumb_up),
                  onPressed: () {},
                ),
                OutlinedButton.icon(
                  icon: Icon(Icons.add),
                  label: Text("添加-alert"),
                  onPressed: () {
                    alertDialog(context);
                  },
                ),
                MyButton(
                  text: "myDeferBut-",
                  width: 350,
                  pressed: () {},
                ),
                DropdownButton<int>(
                  items: [ DropdownMenuItem(
                    child: Text("item1"),
                    value: 1,
                    onTap: () {},
                  ),
                    DropdownMenuItem(
                      child: Text("item2"),
                      value: 2,
                      onTap: () {},
                    )],
                  style: TextStyle(fontSize: 22, color: Colors.green), // 按钮文字样式
                  value: selectedValue, // 按钮默认显示弹框列表的哪个 item，和 DropdownMenuItem 的 value 相对应
                  onChanged: (selected) {
                    print("_MyHomePageState - build onChanged $selected");
                    setState(() {
                     // selectedValue = selected;
                    });
                  },
                  onTap: () {
                    print("_MyHomePageState - build 按钮点击");
                  },
                ),
                SelectWidget(
                  items: [
                    MenuItem(label: "张飞", value: '1'),
                    MenuItem(label: "关羽", value: '2',checked: false),
                    MenuItem(label: "刘备", value: '3'),
                    MenuItem(label: "亚瑟", value: '4'),
                    MenuItem(label: "妲己", value: '5'),
                    MenuItem(label: "兰陵王", value: '6'),
                  ],
                  value: value,
                  valueChanged: selectChange,
                ),
                SelectWidget( //for 循环生成组件
                  items: getMenuItems(),
                  value: value,
                  valueChanged: selectChange,
                ),

              ]),
        ) // This trailing comma makes auto-formatting nicer for build methods.
        );
  }

  getMenuItems(){
    List<MenuItem> list = [];

    list.add(MenuItem(label: "张飞2", value: '1'));
    list.add(MenuItem(label: "关羽2", value: '2'));
    list.add(MenuItem(label: "刘备2", value: '3'));
    list.add(MenuItem(label: "亚瑟2", value: '4'));
    list.add(MenuItem(label: "妲己2", value: '5'));
    list.add( MenuItem(label: "兰陵王2", value: '6',checked: true));
    return list;
  }

  void alertDialog(BuildContext context) async {
    var result = await showDialog(
        barrierDismissible: false, //表示点击灰色背景的时候是否消失弹出框
        context: context,
        builder: (context) {
          return AlertDialog(
            title: const Text("提示信息!"),
            content: const Text("您确定要删除吗"),
            actions: [
              TextButton(
                  onPressed: () {
                    print("ok");
                    Navigator.of(context).pop("ok"); //点击按钮让AlertDialog消失
                  },
                  child: const Text("确定")),
              TextButton(
                  onPressed: () {
                    print("cancel");
                    Navigator.of(context).pop("取消");
                  },
                  child: const Text("取消"))
            ],
          );
        });

    print("-----------");
    print(result);
  }

  void simpleDialog(BuildContext context) async {
    var result = await showDialog(
        barrierDismissible: false, //表示点击灰色背景的时候是否消失弹出框
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: const Text("请选择语言"),
            children: [
              SimpleDialogOption(
                onPressed: () {
                  print("汉语");
                  Navigator.pop(context, "汉语");
                },
                child: const Text("汉语"),
              ),
              const Divider(),
              SimpleDialogOption(
                onPressed: () {
                  print("英语");
                  Navigator.pop(context, "英语");
                },
                child: const Text("英语"),
              ),
              const Divider(),
              SimpleDialogOption(
                onPressed: () {
                  print("日语");
                  Navigator.pop(context, "日语");
                },
                child: const Text("日语"),
              ),
              const Divider(),
            ],
          );
        });
    print(result);
  }

  Future<String> showButtonSheet() async {
    // 默认是弹出50%的
    var result = await showModalBottomSheet(
      isDismissible: true,
      //点击空白处返回功能，默认是true
      enableDrag: true,
      //是否允许拖动
      elevation: 10,
      barrierColor: Colors.grey.withOpacity(0.5),
      //空白处的颜色
      backgroundColor: Colors.white38,
      //背景颜色
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
      //圆角
      context: context,
      builder: (context) {
        return SingleChildScrollView(
          child: Column(
            children: List.generate(
                10,
                (index) => ListTile(
                      title: Text('index=>$index'),
                      onTap: () {
                        Navigator.pop(context, "$index");
                      },
                    )),
          ),
        );
      },
    );

    print(result);
    return result;
  }

  Future<bool> iosSheetShow() async {
    //ios 风格的底部弹出框
    var result = await showCupertinoModalPopup(
        context: context,
        semanticsDismissible: false,
        //是否把屏障加入到屏障树里面？？？
        filter: ImageFilter.blur(sigmaX: 2.0, sigmaY: 3.0),
        //对空白处的颜色进行模糊处理，毛玻璃的效果
        barrierColor: Colors.grey.withOpacity(0.5),
        //空白处的颜色
        builder: (BuildContext context) {
          return CupertinoActionSheet(
            title: const Text('提示'),
            message: const Text('是否要删除当前项？'),
            actions: <Widget>[
              CupertinoActionSheetAction(
                onPressed: () {
                  Navigator.of(context).pop(true);
                },
                isDestructiveAction: true, //红色，危险操作的提示功能

                child: const Text('删除'),
              ),
              CupertinoActionSheetAction(
                onPressed: () {
                  Navigator.of(context).pop(false);
                },
                isDefaultAction: true, //默认选项
                child: const Text('取消'),
              ),
            ],
          );
        });
    if (result != null) {
      print(result);
      return result;
    }
    return false;
  }

  showDropdownMenuItem() {
    print("showDropdownMenuItem");
    List<DropdownMenuItem<String>> sortItems = [];
    String _selectedSort = '排序';

    sortItems.add(DropdownMenuItem(
      value: '排序',
      child: Text('排序'),
      onTap: () {},
    ));
    sortItems.add(
        DropdownMenuItem(value: '价格降序', child: Text('价格降序'), onTap: () {}));
    sortItems.add(
        DropdownMenuItem(value: '价格升序', child: Text('价格升序'), onTap: () {}));

    return DropdownButton(
      value: _selectedSort,
      items: sortItems,
      onChanged: null,
    );
  }
}
